import 'package:bxultimate/app/pages/home/home_search_result/widget/item.dart';
import 'package:bxultimate/app/pages/home/home_search_result/widget/shopItem.dart';
import 'package:bxultimate/widgets/keep_alive.dart';
import 'package:bxultimate/widgets/mk_page.dart';
import 'package:bxultimate/widgets/mk_search_bar.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:tdesign_flutter/tdesign_flutter.dart';

import 'controller.dart';

class HomeSearchResultPage extends StatefulWidget {
  const HomeSearchResultPage({Key? key}) : super(key: key);

  @override
  State<HomeSearchResultPage> createState() => _HomeSearchResultPageState();
}

class _HomeSearchResultPageState extends State<HomeSearchResultPage> {
  final controller = Get.find<HomeSearchResultController>();
  final state = Get.find<HomeSearchResultController>().state;

  @override
  Widget build(BuildContext context) {
    return MkPage(
        child: Scaffold(
          backgroundColor: Colors.transparent,
          appBar: MkSearchBar(
            hintText: '今天搜点什么呢',
            editingController: controller.editingController,
            onPressed: controller.onSearch,
          ),
          body: _buildBody(),
        ));
  }

  _buildBody() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        TDTabBar(
          tabs: [
            for (var item in state.tab)
              TDTab(
                text: item.label,
              )
          ],
          controller: controller.tabController,
          backgroundColor: Colors.transparent,
          showIndicator: true,
          isScrollable: true,
        ),
        Expanded(
            child: TabBarView(
              controller: controller.tabController,
              children: [
                for (var i = 0; i < state.tab.length; i++)
                  _buildPage(state.tab[i].value, i)
              ],
            ))
      ],
    );
  }

  _buildPage(type, index) {
    if(type == "shop"){
      return KeepAliveWrapper(
        child: ShopTabItemView(
          type: type,
          keywords: controller.editingController.text,
        ),
      );
    }
    return KeepAliveWrapper(
      child: TabItemView(
        type: type,
        keywords: controller.editingController.text,
      ),
    );
  }

  @override
  void dispose() {
    Get.delete<HomeSearchResultController>();
    super.dispose();
  }
}